using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

using SBPweb.Modules.Modularity;
using SBPweb.Contracts;
using SBPweb.Contracts.Messaging;
using System.Windows.Forms;

using SBPweb.Modules.Logging;
using SBPweb.Modules.ContactsManager.Controls;
using Verety.SharedMeta.DirectoryServiceMeta;
using SBPweb.Contracts.Implementation;

// This alias is needed for the module loader to recognize the module's entry point
[assembly: AssemblyDefaultAlias("SBPweb.Modules.ContactsManager.ContactsManagerModule")]

namespace SBPweb.Modules.ContactsManager
{
    public class ContactsManagerModule : ModuleBase, IRosterViewer
	{
		#region Private properties

		private ContactListControl pContactListControl;

		#endregion

		#region Public properties

		public static ILogManager LogManager;

        public IModule Module
        {
            get 
            {
                return this;
            }
		}

		#endregion

		#region IRosterViewer Members

		public ContextMenuStrip ContextMenuStrip
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public System.Data.DataRow SelectedRow
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        #endregion

		#region Constructor logic

		public ContactsManagerModule(IModuleHost host)
            : base(host)
        {
            Name = "SBPweb.Modules.ContactsManager";

            LogManager = host.LogManager;

            pIcon = Properties.Resources.Contacts;

            pContactListControl = new ContactListControl(this);

            Services.Add(typeof(IRosterViewer), pContactListControl);
		}

		#endregion

		#region Override methods

		public override void Initialize()
        {
            if (HostType != ModuleHostType.WindowsForms)
            {
				Host.LogManager.WriteError(this, "The HostType is not WinForms!", "RosterViewer");
                return;
            }

            if ((Services[typeof(IMessagingService)] as IMessagingService) == null)
            {
				Host.LogManager.WriteError(this, "There is no IMessagingService in the Services list!", "RosterViewer");
                return;
            }

			LoadPersistency();

            pContactListControl.DirectoryService = (Services[typeof(IMessagingService)] as IMessagingService).ManagedDirectoryServiceObject as IDirectoryService;

            Host.Smartparts.Add(pContactListControl);
        }

		public override void Terminate()
		{
			SavePersistency();
			pContactListControl.Finalize();
			Host.Smartparts.Remove(pContactListControl);
		}

		public override void Dispose()
		{
			Services.Remove(typeof(IRosterViewer));
			pContactListControl.Dispose();
			pContactListControl = null;
			base.Dispose();
		}

		#endregion

		#region Persistency

		public override void LoadPersistency()
		{
			IStatePersistenceService persistenceService = Services.Get<IStatePersistenceService>();
			if (persistenceService != null)
			{
			    PersistencyDictionary state = persistenceService.Load(this);

				if (pContactListControl != null)
				{
					pContactListControl.LoadPersistency(state);
				}
				Host.LogManager.WriteVerbose(this, "Persistency saved for chat module.", "RosterViewer");
			}
		}

		public override void SavePersistency()
		{
			IStatePersistenceService persistenceService = Services.Get<IStatePersistenceService>();
			if (persistenceService != null)
			{
			    PersistencyDictionary state = persistenceService.Load(this);

				if (pContactListControl != null)
				{
					pContactListControl.SavePersistency(state);
				}
				persistenceService.Save(this, state);
				Host.LogManager.WriteVerbose(this, "Persistency loaded for chat module.", "RosterViewer");
			}
		}

		#endregion
	}
}
